/*
 * @Description: 路由入口文件
 * @Version: 1.0
 * @Autor: wq
 * @Date: 2021-08-04 13:22:43
 * @LastEditors: wq
 * @LastEditTime: 2021-08-06 14:44:05
 * @FilePath: \vue_project\mmpt\src\router\index.js
 */
import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import Layout from "@/layout";
import { getToken } from "@/utils/auth";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import(/* webpackChunkName: "login" */ "../views/Login"),
  },
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "home",
        component: () => import(/* webpackChunkName: "home" */ "@/views/Home"),
        name: "Home",
        meta: { title: "首页", keepAlive: false },
      },
    ],
  },
  {
    path: "/test",
    component: Layout,
    redirect: '/test/start',
    children: [
      {
        path: "start",
        component: () => import(/* webpackChunkName: "Test" */ "@/views/Test"),
        name: "Test",
        meta: { title: "开始测评", keepAlive: false },
        redirect: '/test/start/relative',
        children: [
          {
            path: "relative",
            component: () => import(/* webpackChunkName: "relative" */ "@/views/Relative"),
            name: "Relative",
            meta: { title: "关联被测人", keepAlive: false },
            children: [
              {
                path: "selectFace",
                component: () => import(/* webpackChunkName: "relative" */ "@/views/Relative/Face"),
                name: "selectFace",
                meta: { title: "选择人脸识别", keepAlive: false },
              }
            ]
          },
          {
            path: "selection",
            component: () => import(/* webpackChunkName: "Selection" */ "@/views/Selection"),
            name: "Selection",
            meta: { title: "选择量表", keepAlive: false },
          }
        ]
      },
      {
        path: "record",
        component: () => import(/* webpackChunkName: "Record" */ "@/views/Record"),
        name: "Record",
        meta: { title: "测评记录", keepAlive: false },
      },
    ],
  },
  {
    path: "/404",
    name: "NotFound",
    component: () =>
      import(/* webpackChunkName: "NotFound" */ "../views/NotFound"),
  },
  {
    path: "*",
    redirect: { path: "/404" },
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

// 路由守卫，对没有登录进行拦截
router.beforeEach((to, from, next) => {
  NProgress.start();
  if (to.path === "/login") {
    return next();
  }
  const tokenStr = getToken();
  if (!tokenStr) {
    Vue.prototype.$message({
      type: "error",
      message: "登录信息失效,请重新登录！",
    });
    return next("/login");
  }
  next();
});
router.afterEach(() => {
  NProgress.done();
});

// 解决重复点击路由跳转，控制台warning
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error);
};

export default router;
